import * as utils from './util.js';
import CONFIG from '../config.js';

const loginUrl = require('../config').loginUrl
const jxListUrl = require('../config').jxListUrl
const tongJiUrl = require('../config').tongJiUrl
const syUrl = require("../config").syUrl
const detailUrl = require("../config").detailUrl
const joinDBLists = require('../config').joinDBLists
const rankUrl = require('../config').rankUrl
const joinCJUrl = require('../config').joinCJUrl
const allListUrl = require('../config').allListUrl
const updateUserInfoUrl = require('../config').updateUserInfoUrl
const myinfoUrl = require('../config').myinfoUrl
const zjRecord = require('../config').zjRecord
const cjRecord = require('../config').cjRecord
const submitAddress = require('../config').submitAddress
const helpList = require('../config').helpList
const recordShareUrl = require('../config').recordShareUrl

let app = getApp();
// let DOMIAN = CONFIG[CONFIG.WX_ENV];
const getUserId = () => {
  var userInfo = app.globalData.userInfo ? app.globalData.userInfo : app.getUserInfo();
  var user_id = userInfo ? userInfo.UserId : '';
  console.log('getUserId', user_id)
  return user_id;
};
const showToast = (title) => {
  if (title == '') return;
  wx.showToast({
    title: title,
    icon: 'none',
    duration: 2000
  })
}

let S_request = {
  jx: {
    getJXList: function (userId, treasure_id, cb) {
      let token = wx.getStorageSync("token");
      if(token == '')return;
      // console.log("token==" + token)
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: jxListUrl,
        data: { //请求的参数
          token: token,
          fu: userId,
          treasure_id: treasure_id
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg);
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          console.log(err)
        }
      })
    },
    getDetailInfo: function(id, cb) {
      let token = wx.getStorageSync("token");
      // console.log("token==" + token)
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: detailUrl,
        data: { //请求的参数
          token: token,
          id: id
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          console.log(err)
        }
      })
    },
    getSYInfo: function(name, mobile, qq, desc, cb) {
      let token = wx.getStorageSync("token");
      // console.log("token==" + token)
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: syUrl,
        data: { //请求的参数
          token: token,
          contact_name: name,
          contact_mobile: mobile,
          contact_qq:qq,
          desc: desc
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          console.log(err)
        }
      })
    },
    getJoinDBList: function(page, id, cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: joinDBLists,
        data: { //请求的参数
          token: token,
          page: page,
          id: id
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          console.log(err)
        }
      })
    },
    getRankList: function(id, cb) {
      let token = wx.getStorageSync("token");
      // console.log("token==" + token)
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: rankUrl,
        data: { //请求的参数
          token: token,
          id: id
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          console.log(err)
        }
      })
    },
    joinCJ: function(id, cb) {
      let token = wx.getStorageSync("token");
      // console.log("token==" + token)
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: joinCJUrl,
        data: { //请求的参数
          token: token,
          id: id
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          console.log(err)
        }
      })
    },
    getAllList: function(page, cb) {
      let token = wx.getStorageSync("token");
      // console.log("token==" + token)
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: allListUrl,
        data: { //请求的参数
          token: token,
          page: page
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          console.log(err)
        }
      })
    },
    updateUserInfo: function(userInfo, cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        method: 'POST', //默认是GET
        url: updateUserInfoUrl,
        data: { //请求的参数
          token: token,
          nickname: userInfo.nickName,
          head_icon: userInfo.avatarUrl,
          userinfo: JSON.stringify(userInfo)
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            // 用户id，分享的时候用
            wx.setStorageSync("userId", result.data.id);
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
    recordShare: function(id, obj, cb) {
      let token = wx.getStorageSync("token");
      obj.token = token
      obj.redPackId = id
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: recordShareUrl,
        data: obj,
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
    tongJiNum: function(id, param, cb) {
      let token = wx.getStorageSync("token");
      console.log("param==" + param)
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: tongJiUrl + "?token=" + token + "&id=" + id + "&" + param,
        // data: { //请求的参数
        //   token: token,
        //   id: id,
        //   param: param
        // },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
    getUserInfo: function(cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        // method: 'POST', //默认是GET
        url: myinfoUrl,
        data: { //请求的参数
          token: token
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    }
  },
  mine: {
    myCJInfo: function(cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        url: myinfoUrl,
        data: { //请求的参数
          token: token
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
    zjRecord: function(cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        url: zjRecord,
        data: { //请求的参数
          token: token
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
    cjRecord: function(cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        url: cjRecord,
        data: { //请求的参数
          token: token
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
    excJP: function(id, contact_mobile, contact_name, contact_address, cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        url: submitAddress,
        data: { //请求的参数
          token: token,
          id:id,
          contact_name: contact_name,
          contact_mobile: contact_mobile,
          contact_address: contact_address
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
    getHelpList: function(cb) {
      let token = wx.getStorageSync("token");
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        url: helpList,
        data: { //请求的参数
          token: token
        },
        success: (res) => {
          let result = res.data;
          if (result.code == 0) {
            typeof cb == "function" && cb(result.data);
          } else {
            console.log(result.msg)
            showToast(result.msg);
          }
        },
        fail: (err) => {
          err.statusCode = CONFIG.CODE.REQUESTERROR;
          //  typeof cb == "function" && cb(err);
          console.log(err)
        }
      })
    },
  },
  login: function(code, cb) {
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST', //默认是GET
      url: loginUrl,
      data: {
        code: code
      },
      success: function(res) {
        // debugger;
        console.log("login1111====" + JSON.stringify(res.data));
        let tempData = res.data
        //token存储在本地
        wx.setStorageSync("token", tempData.data.token);
        // 用户id，分享的时候用
        wx.setStorageSync("userId", tempData.data.user.id);
        cb(null)
      },
      fail: function(res) {
        console.log('获取token失败', res)
        cb(res)
      }
    })
  }
};
module.exports = S_request;